﻿// <copyright file="StringToImageConverter.cs" company="$registerdorganization$">
// Copyright (c) 2011 . All Right Reserved
// </copyright>
// <author>RICARDOPONS</author>
// <email></email>
// <date>2011-07-23</date>
// <summary>A value converter for WPF and Silverlight data binding</summary>

namespace ShoppingList.Converters
{
    using System;
    using System.Windows.Data;
    using System.Windows.Media.Imaging;
    using System.IO.IsolatedStorage;
    using System.IO;

    /// <summary>
    /// A Value converter
    /// </summary>
    public class StringToImageConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI. 
        /// </summary>
        /// <param name="value">The source data being passed to the target </param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property. </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           
            string path = (string)value;

            if (!string.IsNullOrEmpty(path))
            {
               

              BitmapImage img=  Helpers.DataBaseHelpers.LoadImage(path);

              if (img != null) 
              {
                  return img; 
              } 
              else
              { 
                  return new BitmapImage(new Uri("/icons/appbar.favs.rest.png",UriKind.Relative));
              }
            
                }
            else
            {
                return new BitmapImage(new Uri("/icons/appbar.favs.rest.png", UriKind.Relative));
            }

          

           
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object. This method is called only in TwoWay bindings. 
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic. </param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the source object.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }
}
